#delimit;
clear all;
set more off;
set logtype text;
capture log close table_pollutionsum;

** REPLACE FILE PATH WITH PATH TO RELEVANT REPLICATION FILES;
local fileloc = "~/KMS_REPLICATION";

log using `fileloc'/log_files/table_pollutionsum.txt, replace name(table_pollutionsum);


**XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX;
** TABLES 1 AND 2; 
**XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX;

** Run regression prep;
do `fileloc'/all_regression_prep_hazard_collapsed.do `fileloc' _all;

** Sample 1 by zip code and week (avoid multiple mother/zip/week observations driving mean weather/traffic values);
bysort mother_zip merging_week: keep if _n == 1;

** Variable labels;
label variable weekly_co "CO";
label variable weekly_pm10 "PM10";
label variable weekly_oz "O3";
label variable rain "Rainfall";
label variable max_temp "Max Temp";
label variable windspeed "Wind Speed";
label variable humidS "Specific Humidity";
label variable days_with_rain "Rainy Days";
label variable days_with_fog "Foggy Days";


** Pollution summary;
estpost tabstat weekly_co weekly_pm10 rain max_temp windspeed humidS days_with_rain days_with_fog , statistics(mean) listwise by(event_year);

estout using `fileloc'/regs/pollution_means.tex,
	style(tex)
	replace
	cells("weekly_co(fmt(%9.2f)) weekly_pm10(fmt(%9.2f)) rain(fmt(%9.2f)) max_temp(fmt(%9.2f)) windspeed(fmt(%9.2f)) humidS(fmt(%9.2f)) days_with_rain(fmt(%9.2f)) days_with_fog(fmt(%9.2f))")
	label;

** Traffic summary;
estpost tabstat flow_by_length_5 flow_by_length_10 flow_by_length_15 flow_by_length_20 tot_flow_base, statistics(mean) listwise by(event_year);

estout using `fileloc'/regs/traffic_means.tex,
	style(tex)
	replace
	cells("flow_by_length_5(fmt(%9.2f)) flow_by_length_10(fmt(%9.2f)) flow_by_length_15(fmt(%9.2f)) flow_by_length_20(fmt(%9.2f)) tot_flow_base(fmt(%9.2f))")
	label;

*** Standard deviations;
egen zip_month = group(mother_zip);

xtset zip_month;

eststo clear;
foreach var in weekly_co weekly_pm10 rain max_temp windspeed humidS days_with_rain days_with_fog {;
	qui reg `var';
	qui xtsum `var';
	estadd scalar real_mean = r(mean);
	estadd scalar overall_sd = r(sd);
	estadd scalar within_sd = r(sd_w);
	estadd scalar between_sd = r(sd_b);
	eststo, title("`var'");
};

esttab,
	scalar("real_mean Mean" "overall_sd Overall SD" "within_sd Within SD" "between_sd Between SD");

estout using `fileloc'/regs/deviations_weather.tex,
	style(tex)
	label
	replace
	stats(real_mean overall_sd within_sd between_sd, label("Mean" "Overall SD" "Within SD" "Between SD") fmt(%9.2f %9.2f %9.2f %9.2f));


xtset zip_month;

eststo clear;
foreach var in flow_by_length_5 flow_by_length_10 flow_by_length_15 flow_by_length_20 tot_flow_base {;
	qui reg `var';
	qui xtsum `var';
	estadd scalar real_mean = r(mean);
	estadd scalar overall_sd = r(sd);
	estadd scalar within_sd = r(sd_w);
	estadd scalar between_sd = r(sd_b);
	eststo, title("`var'");
};

esttab,
	scalar("real_mean Mean" "overall_sd Overall SD" "within_sd Within SD" "between_sd Between SD");

estout using `fileloc'/regs/deviations_traffic.tex,
	style(tex)
	label
	replace
	stats(real_mean overall_sd within_sd between_sd, label("Mean" "Overall SD" "Within SD" "Between SD") fmt(%9.2f %9.2f %9.2f %9.2f));

log close table_pollutionsum;
